package com.liusu.spider.test;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
import org.junit.jupiter.api.Test;

import java.io.IOException;

public class HTTPConfig {
    /**
     * HTTP客户端连接池的使用
     */
    @Test
    public void context(){

        PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager();
        send_get(manager);
        manager.setMaxTotal(100);
        /**
         * 每个域名的最大连接数
         */
        manager.setDefaultMaxPerRoute(10);
    }

    public void send_get(PoolingHttpClientConnectionManager manager) {

        CloseableHttpClient client = HttpClients.custom().setConnectionManager(manager).build();
        String url = "http://www.itcast.cn/";
        HttpGet get = new HttpGet(url);
        RequestConfig config = RequestConfig.custom().setConnectTimeout(1000).setConnectionRequestTimeout(500).setSocketTimeout(10*1000).build();
        get.setConfig(config);
        get.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3870.400 QQBrowser/10.8.4405.400");

        try {
            CloseableHttpResponse response = client.execute(get);
            String html = EntityUtils.toString(response.getEntity(),"utf-8");
            System.out.println(html);
            response.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
